//
//  MemberCardTopUpViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 16/3/24.
//  Copyright © 2016年 reabam.com. All rights reserved.
//


#import "MemberCardTopUpViewController.h"
#import "cardView.h"
#import "ChoosePaymentViewController.h"
#import "MJRefresh.h"
#import "huiyuanDetailsCell.h"
#import "HZAreaPickerView.h"
#import "ZJSwitch.h"
#import "CardView.h"
#import "MemberCardRecordsViewController.h"
@interface MemberCardTopUpViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UIScrollViewDelegate,UITextViewDelegate>
@end

@implementation MemberCardTopUpViewController


{ UILabel*miane;
    CardView *View;
    UIView*heardView;
    int d;
    UIView*v;
    NSMutableArray*arr;
    UIButton*tureBtn;
    UIScrollView*mainScroll;
}
@synthesize alt = alt;
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated{
    
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)ToObtain:(UIButton*)sender{
    MemberCardRecordsViewController * Login=[[MemberCardRecordsViewController alloc]init];
    Login.memberId=self.memberId;
    [self.navigationController pushViewController:Login animated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];

    self.title = @"会员储值";
   [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    self.view.backgroundColor=[UIColor whiteColor];
    
    heardView=[[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, 120)];
    heardView.backgroundColor=luseColor;
    [self.view addSubview:heardView];
   UILabel* priceLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 35, ScreenWidth,32)];
    priceLabel.text=[NSString stringWithFormat:@"¥ %@", self.price];
    priceLabel.font=[UIFont systemFontOfSize:32];
    priceLabel.textAlignment=NSTextAlignmentCenter;
    priceLabel.textColor=[UIColor whiteColor];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥ %@", self.price]];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:25]
     
                          range:NSMakeRange(0, 1)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor whiteColor]
     
                          range:NSMakeRange(0, 1)];
    priceLabel.attributedText = AttributedStr;
    [heardView addSubview:priceLabel];
    
    UIButton* ToObtain = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth/2-45, priceLabel.bottom+12, 90,25)];
    [ToObtain setTitle:@"储值卡余额" forState:UIControlStateNormal];
    ToObtain.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    [ToObtain.layer setCornerRadius:13]; //设置矩圆角半径
    [ToObtain.layer setBorderWidth:0.5];   //边框宽度
    CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){255/255.0, 255/255.0, 255/255.0, 0.6});
    [ToObtain.layer setBorderColor:tureBTncolorref];//边框颜色
    [ToObtain setTitleColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1]
                   forState:UIControlStateNormal];
    [ToObtain addTarget:self action:@selector(ToObtain:) forControlEvents:UIControlEventTouchUpInside];
    [heardView addSubview:ToObtain];

    miane=[[UILabel alloc]initWithFrame:CGRectMake(15, heardView.bottom+25, ScreenWidth-100, 16)];
    miane.text=@"充值面额";
    miane.textColor=qianheiColor;
    miane.font=[UIFont systemFontOfSize:15];
    [self.view addSubview:miane];
    
    [mainScroll removeFromSuperview];
    
    arr=[[NSMutableArray alloc]init];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    

    
    mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,heardView.bottom+50, ScreenWidth,ScreenHeight)];
    mainScroll.delegate = self;
    mainScroll.userInteractionEnabled = YES;
    mainScroll.scrollEnabled = YES;
    mainScroll.backgroundColor=[UIColor whiteColor];
    [self .view addSubview:mainScroll];
    
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    
    
    [HttpRequest requestData:@"Business/Member/CardOptions" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
              NSLog(@"aaaaaaaa%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
          
                arr=[severDataDict.dataDict objectForKey:@"DataLine"];
                for (int i = 0,j = 0; i < arr.count; i++) {
                    if (i % 3 == 0) {
                        j++;
                    }
                 
                    View = [[CardView alloc]initWithFrame:CGRectMake(12.5+(ScreenWidth/3-5)*(i%3), (90*j)-80, ScreenWidth/3-15, 80)];
                    View.buttonView.titleLabel.textAlignment=NSTextAlignmentCenter;
                    View.buttonView.titleLabel.font=[UIFont systemFontOfSize:24];
                    View.buttonView.tag=i;
                    
                    NSString*s=[NSString stringWithFormat:@"%@元", [[arr objectAtIndex:i] objectForKey:@"faceValue"]];
                   
                    [View.buttonView setTitle:s forState:UIControlStateNormal];
                    
                    NSMutableAttributedString *AttributedStr1= [[NSMutableAttributedString alloc]initWithString:s];
                    
                    [AttributedStr1 addAttribute:NSFontAttributeName
                     
                                          value:[UIFont systemFontOfSize:18]
                     
                                          range:NSMakeRange(s.length-1, 1)];
                    
                    [AttributedStr1 addAttribute:NSForegroundColorAttributeName
                     
                                          value:luseColor
                     
                                          range:NSMakeRange(s.length-1, 1)];
                    
                    
                    View.buttonView.titleLabel.attributedText = AttributedStr1;

               
                    
                    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
                    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){90/255.0,130/255.0, 26/255.0, 1.0   });
                    [View.buttonView.layer setBorderColor:colorref2];//边框颜色
                    [View.buttonView.layer setCornerRadius:3.0];
                    [View.buttonView.layer setBorderWidth:0.43];
                    [View.buttonView addTarget:self action:@selector(dianju:) forControlEvents:UIControlEventTouchUpInside];
                    [View.buttonView setTitleColor:luseColor forState:0];
                    
                    View.label_price.text=[NSString stringWithFormat:@"销价 ¥ %@元", [[arr objectAtIndex:i] objectForKey:@"saleValue"]];
                   
                    [mainScroll addSubview:View];
                    
                   
                    
                }
                
                [mainScroll setContentSize:CGSizeMake(ScreenWidth, View.bottom+64)];
                
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            NSLog(@"aaaaaaaa%@",severDataDict.dataDict);
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
    [tureBtn removeFromSuperview];
    tureBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, ScreenHeight-48, ScreenWidth, 48)];
    [tureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [tureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [tureBtn addTarget:self action:@selector(ture:) forControlEvents:UIControlEventTouchUpInside];
    tureBtn.backgroundColor=qianhuiColor;
    [self.view addSubview:tureBtn];
}
-(void)dianju:(UIButton*)sender{

    NSString*tag=[NSString stringWithFormat:@"%ld",sender.tag];
    d=tag.intValue;
    [self jiazai];
    
    [tureBtn removeFromSuperview];
    tureBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, ScreenHeight-48, ScreenWidth, 48)];
    [tureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [tureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [tureBtn addTarget:self action:@selector(ture:) forControlEvents:UIControlEventTouchUpInside];
    tureBtn.backgroundColor=luseColor;
    [self.view addSubview:tureBtn];
   
    
}
-(void)jiazai{
    [mainScroll removeFromSuperview];
    mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,heardView.bottom+50, ScreenWidth,ScreenHeight)];
    mainScroll.delegate = self;
    mainScroll.userInteractionEnabled = YES;
    mainScroll.scrollEnabled = YES;
    mainScroll.backgroundColor=[UIColor whiteColor];
    [self .view addSubview:mainScroll];

    for (int i = 0,j = 0; i < arr.count; i++) {
        if (i % 3 == 0) {
            j++;
        }
        
        View = [[CardView alloc]initWithFrame:CGRectMake(12.5+(ScreenWidth/3-5)*(i%3), (90*j)-80, ScreenWidth/3-15, 80)];
        View.buttonView.titleLabel.textAlignment=NSTextAlignmentCenter;
        View.buttonView.titleLabel.font=[UIFont systemFontOfSize:24];
        View.buttonView.tag=i;
        NSString*s=[NSString stringWithFormat:@"%@元", [[arr objectAtIndex:i] objectForKey:@"faceValue"]];
        
        [View.buttonView setTitle:s forState:UIControlStateNormal];
        
        NSMutableAttributedString *AttributedStr1= [[NSMutableAttributedString alloc]initWithString:s];
        
        [AttributedStr1 addAttribute:NSFontAttributeName
         
                               value:[UIFont systemFontOfSize:18]
         
                               range:NSMakeRange(s.length-1, 1)];
        
        [AttributedStr1 addAttribute:NSForegroundColorAttributeName
         
                               value:luseColor
         
                               range:NSMakeRange(s.length-1, 1)];
        
        
        View.buttonView.titleLabel.attributedText = AttributedStr1;
        CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
        CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){90/255.0,130/255.0, 26/255.0, 1.0   });
        [View.buttonView.layer setBorderColor:colorref2];//边框颜色
        [View.buttonView addTarget:self action:@selector(dianju:) forControlEvents:UIControlEventTouchUpInside];
        [View.buttonView setTitleColor:luseColor forState:0];
        [View.buttonView.layer setCornerRadius:3.0];
        [View.buttonView.layer setBorderWidth:0.43];
        View.label_price.text=[NSString stringWithFormat:@"销价 ¥ %@元", [[arr objectAtIndex:i] objectForKey:@"saleValue"]];
        if (i==d) {
            View.buttonView.backgroundColor=luseColor;
            View.label_price.textColor=[UIColor whiteColor];
            [View.buttonView setTitleColor:[UIColor whiteColor] forState:0];
        }
        
         [mainScroll addSubview:View];
    }
    
    [mainScroll setContentSize:CGSizeMake(ScreenWidth, View.bottom+64)];

}
-(void)ture:(UIButton*)sender{
    
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString* Token = [user objectForKey:@"tokenId"];
            NSMutableDictionary *params = [NSMutableDictionary new];
            [params setValue:Token forKey:@"tokenId"];
            [params setValue:self.memberId forKey:@"memberId"];
            [params setValue:[[arr objectAtIndex:d] objectForKey:@"coId"]  forKey:@"coId"];
            [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
            
            
            [HttpRequest requestData:@"Business/Member/CardTopUp" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                    
                    NSLog(@"33333333====%@",severDataDict.dataDict);
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        [Global hideProgressHUDInternal:self.view];
                        ChoosePaymentViewController  *generateVC=[[ChoosePaymentViewController alloc] init];
                        generateVC.orderSat=@"CardTopUp";
                        generateVC.memberId=self.memberId;
                        generateVC.Dict=severDataDict.dataDict;
                        [self.navigationController pushViewController:generateVC animated:YES];

                        
                    }else{
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        [Global showTextHudSub:requestResultStr view:self.view];
                    }
                    
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    [Global showTextHudSub:@"连接失败" view:self.view];
                }
                
            }];
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
@end
